﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
#if UNITY_EDITOR
public class MapEdit : MonoBehaviour {

    public List<Vector3> roadPoit = new List<Vector3>();

	// Use this for initialization
	void Start () {
    }
	
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0))
        {
            Vector3 pos = Input.mousePosition;            
            pos = Camera.main.ScreenToWorldPoint(pos);
            pos.y = 0;
            pos.x = Mathf.RoundToInt(pos.x);
            pos.z = Mathf.RoundToInt(pos.z);
            roadPoit.Add(pos);
        }
        if (Input.GetMouseButtonDown(1))
        {
            Vector3 pos = Input.mousePosition;
            pos = Camera.main.ScreenToWorldPoint(pos);
            pos.y = 0;
            pos.x = Mathf.RoundToInt(pos.x);
            pos.z = Mathf.RoundToInt(pos.z);
            roadPoit.Remove(pos);
        }
        if(Input.GetAxis("Mouse ScrollWheel")<0)
        {
            Camera.main.orthographicSize *= 0.9f;
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            Camera.main.orthographicSize *= 1.1f;
        }
        if(Input.GetKey(KeyCode.W))
        {
            Vector3 pos = Camera.main.transform.localPosition;
            pos.z += 1;
            Camera.main.transform.localPosition = pos;
        }
        if (Input.GetKey(KeyCode.S))
        {
            Vector3 pos = Camera.main.transform.localPosition;
            pos.z -= 1;
            Camera.main.transform.localPosition = pos;
        }
        if (Input.GetKey(KeyCode.D))
        {
            Vector3 pos = Camera.main.transform.localPosition;
            pos.x += 1;
            Camera.main.transform.localPosition = pos;
        }
        if (Input.GetKey(KeyCode.A))
        {
            Vector3 pos = Camera.main.transform.localPosition;
            pos.x -= 1;
            Camera.main.transform.localPosition = pos;
        }
        if(Input.GetKey(KeyCode.F2))
        {
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<roadPoit.Count;i++)
            {
                sb.AppendLine(string.Format("{0},{1}", roadPoit[i].x, roadPoit[i].z));
            }
            string filePath = EditorUtility.SaveFilePanel("保存路径文件", ".", DateTime.Now.ToString("yyyyMMddHHmmm"), "txt");
            File.WriteAllText(filePath, sb.ToString());
        }
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        if(roadPoit.Count>0)
        {
            Gizmos.DrawCube(roadPoit[0], Vector3.one);
        }
        if(roadPoit.Count<1)
        {
            return;
        }
        int _x = (int)roadPoit[0].x;
        int _z = (int)roadPoit[0].z;
        Gizmos.color = Color.red;
        for(int i=1;i<roadPoit.Count;i++)
        {
            if(_x!=roadPoit[i].x)
            {
                _x = (int)roadPoit[i].x;
                Gizmos.DrawLine(roadPoit[i - 1], new Vector3(_x, 0.5f, _z));
            }
            if (_z != roadPoit[i].z)
            {
                Gizmos.DrawLine(new Vector3(_x, 0.5f, _z), roadPoit[i]);
                _z = (int)roadPoit[i].z;
            }
        }
        Gizmos.color = Color.yellow;
        Gizmos.DrawCube(roadPoit[roadPoit.Count - 1], Vector3.one);

    }
}
#endif